package com.caffedev.ottollotto;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class OttollottoAddNum extends Activity {

	private Spinner spin;
	private Button btn_del_magic;
	private Button btn_del;
	private Button btn_add;
	private Button btn_cancel;
	//private TextView selected;
	private ImageView selected1;
	private ImageView selected2;
	private ImageView selected3;
	private ImageView selected4;
	private ImageView selected5;
	private ImageView selected6;
	private EditText magic;
	
	private int ballList_index = 0;
	private int[] ballList={0,0,0,0,0,0};
	
	private static final String KEY_ID="_ID";
	private static final String KEY_GRWNO="grwno";
	private static final String KEY_GRWDATE="grwdate";
	private static final String KEY_FIRSTBALL="firstball";
	private static final String KEY_SECONDBALL="secondball";
	private static final String KEY_THIRDBALL="thirdball";
	private static final String KEY_FOURTHBALL="fourthball";
	private static final String KEY_FIFTHBALL="fifthball";
	private static final String KEY_SIXTHBALL="sixthball";
	private static final String KEY_ADD_DATE="adddate";
	private static final String KEY_RESULT="result";
	private static final String KEY_MAGIC = "magic";
	
	private static final String TABLE_NAME = "sTable";
	
	private ArrayAdapter<String> adspin;
	private List<String> lines;
	private String add_grwno;
	
	private GridView grid;
	private ImageAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.addnum);
		
		magic = (EditText)findViewById(R.id.magic);

		spin = (Spinner)findViewById(R.id.spinner1);
		
		btn_del_magic = (Button)findViewById(R.id.btn_del_magic);
		btn_del = (Button)findViewById(R.id.btn_del);
		btn_add = (Button)findViewById(R.id.btn_add);
		btn_cancel = (Button)findViewById(R.id.btn_cancel);
		/*selected = (TextView)findViewById(R.id.mynum);
		selected.setText("");*/
		selected1 = (ImageView)findViewById(R.id.imageView1);
		selected2 = (ImageView)findViewById(R.id.imageView2);
		selected3 = (ImageView)findViewById(R.id.imageView3);
		selected4 = (ImageView)findViewById(R.id.imageView4);
		selected5 = (ImageView)findViewById(R.id.imageView5);
		selected6 = (ImageView)findViewById(R.id.imageView6);
		
		ballList_index=0;
		
		
		int no = Integer.valueOf(OttollottoMain.GRWNO)+1;
		
		lines = new ArrayList<String>();
		
		for(int i = 0; i<=99 ; i++){
			lines.add(Integer.toString(no));
			no--;
		}
		
		adspin = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,lines);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		
		spin.setAdapter(adspin);
		
		grid = (GridView)findViewById(R.id.gridView1);
		
		adapter = new ImageAdapter(this);
		grid.setAdapter(adapter);
		
		spin.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				add_grwno = lines.get(arg2);
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
			
		});
		btn_del_magic.setOnClickListener(listener);
		btn_del.setOnClickListener(listener);
		btn_add.setOnClickListener(listener);
		btn_cancel.setOnClickListener(listener);
		
		grid.setOnItemClickListener(add_listener);
		
		
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
		Intent intent = getIntent();
		

	}
	
	private boolean isInList(int ball){
		for(int i=0; i<=5;i++){
			if(ballList[i]==ball){
				return true;
			}
		}
		return false;
	}
	private int[] ball = {R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,R.drawable.b5,
			R.drawable.b6,R.drawable.b7,R.drawable.b8,R.drawable.b9,R.drawable.b10,
			R.drawable.b11,R.drawable.b12,R.drawable.b13,R.drawable.b14,R.drawable.b15,
			R.drawable.b16,R.drawable.b17,R.drawable.b18,R.drawable.b19,R.drawable.b20,
			R.drawable.b21,R.drawable.b22,R.drawable.b23,R.drawable.b24,R.drawable.b25,
			R.drawable.b26,R.drawable.b27,R.drawable.b28,R.drawable.b29,R.drawable.b30,
			R.drawable.b31,R.drawable.b32,R.drawable.b33,R.drawable.b34,R.drawable.b35,
			R.drawable.b36,R.drawable.b37,R.drawable.b38,R.drawable.b39,R.drawable.b40,
			R.drawable.b41,R.drawable.b42,R.drawable.b43,R.drawable.b44,R.drawable.b45
	};
	private AdapterView.OnItemClickListener add_listener =  new AdapterView.OnItemClickListener() {
		public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
			
			if(isInList(position+1)){
				//Toast.makeText(OttollottoMyNum.this,R.string.already_selected, Toast.LENGTH_SHORT).show();
			}else{
				if(ballList_index<=5){
					ballList[ballList_index++] = position+1;
					//list.setText( list.getText()+" "+ getString(position+1));
					switch(ballList_index){
					case 1: selected1.setBackgroundResource(getResources().getIdentifier("c" +(position+1), "drawable", getPackageName())); break;
					case 2: selected2.setBackgroundResource(getResources().getIdentifier("c" +(position+1), "drawable", getPackageName())); break;
					case 3: selected3.setBackgroundResource(getResources().getIdentifier("c" +(position+1), "drawable", getPackageName())); break;
					case 4: selected4.setBackgroundResource(getResources().getIdentifier("c" +(position+1), "drawable", getPackageName())); break;
					case 5: selected5.setBackgroundResource(getResources().getIdentifier("c" +(position+1), "drawable", getPackageName())); break;
					case 6: selected6.setBackgroundResource(getResources().getIdentifier("c" +(position+1), "drawable", getPackageName())); break;
						
					}
//					selected.setText( selected.getText()+" "+Integer.toString(position+1));
					v.setBackgroundResource(getResources().getIdentifier("c" +(position+1), "drawable", getPackageName()));
					
				}
			}
			
		}
		
	};
private View.OnClickListener listener = new View.OnClickListener() {
		
		public void onClick(View v) {
			switch(v.getId()){

			case R.id.btn_add:
				if(ballList_index<=5){
					Toast.makeText(OttollottoAddNum.this,R.string.need_number, Toast.LENGTH_SHORT).show();
					break;
				}
				addNewItem();
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				finish();

				break;
				
			case R.id.btn_cancel:
				finish();
				
				break;
			case R.id.btn_del:
				for(int i = 0; i<=5 ; i++)
					ballList[i]=0;
				ballList_index=0;
				selected1.setBackgroundResource(R.drawable.color1);
				selected2.setBackgroundResource(R.drawable.color1);
				selected3.setBackgroundResource(R.drawable.color1);
				selected4.setBackgroundResource(R.drawable.color1);
				selected5.setBackgroundResource(R.drawable.color1);
				selected6.setBackgroundResource(R.drawable.color1);
				//selected.setText("");
				adapter.notifyDataSetChanged();

				break;
			case R.id.btn_del_magic:
				magic.setText("");
				break;
			}

			
		}
	};
	
	private void addNewItem(){
		
		Calendar cal=Calendar.getInstance();
		
		ContentValues arg = new ContentValues();
		//arg.put(KEY_GRWNO, OttollottoMain.GRWNO);
		arg.put(KEY_GRWNO, add_grwno);
		arg.put(KEY_GRWDATE, OttollottoMain.GRWDate);
		arg.put(KEY_FIRSTBALL, Integer.toString(ballList[0]));
		arg.put(KEY_SECONDBALL, Integer.toString(ballList[1]));
		arg.put(KEY_THIRDBALL, Integer.toString(ballList[2]));
		arg.put(KEY_FOURTHBALL, Integer.toString(ballList[3]));
		arg.put(KEY_FIFTHBALL, Integer.toString(ballList[4]));
		arg.put(KEY_SIXTHBALL, Integer.toString(ballList[5]));
		arg.put(KEY_ADD_DATE, cal.get(Calendar.YEAR)+"/"+cal.get(Calendar.MONTH)+"/"+cal.get(Calendar.DAY_OF_MONTH));
		arg.put(KEY_RESULT, "0");
		if(magic.getText().toString().equals(""))
			arg.put(KEY_MAGIC, getString(R.string.bless));
		else
			arg.put(KEY_MAGIC, magic.getText().toString());
		
		long rowId = OttollottoMain.mDb.insert(TABLE_NAME, null, arg);
	}
	
////////////custom imageadapter
public class ImageAdapter extends BaseAdapter{
private Context mContext;

public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
return ball.length;
}
public Object getItem(int position) {
return ball[position];
}

public long getItemId(int position) {
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView == null){
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(45,45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}else{
imageView = (ImageView)convertView;
}
imageView.setBackgroundResource(ball[position]);
return imageView;
}

}


}

